package com.admission.web.admin;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.ArtForm;
import com.admission.service.ArtDetailManager;
import com.admission.service.ArtFormManager;
import com.admission.util.NewFileName;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;

@Namespace("/admin")
public class ArtFormAdminAction extends CrudActionSupport<ArtForm> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArtFormManager artFormManager;
	private String id;
	private String ids;
	private ArtForm entity;
	private Page<ArtForm> page = new Page<ArtForm>(PageUtils.DEFAULT_PAGE_SIZE);
	private File artFile;
	private String artFileFileName;
	private ArtDetailManager artDetailManager;
	private String keywords = "";
	private String status;

	public ArtForm getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		if (keywords != "") {
			PropertyFilter p = new PropertyFilter("LIKES_title", keywords);
			filters.add(p);
		}
		page.setOrder(Page.DESC);
		page.setOrderBy("inputTime");
		page = artFormManager.getPage(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		String uploadPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		String subPath = null;
		try {
			if (artFile != null) {
				subPath = new String();
				subPath = NewFileName.genNewFileName(artFileFileName, 0);
				FileUtils.copyFile(artFile, new File(uploadPath, subPath));
				entity.setTitle(artFileFileName.substring(0, artFileFileName.lastIndexOf(".")));
				artFormManager.save(entity, subPath);
				artDetailManager.save(uploadPath + File.separator + subPath,
						entity.getId());
			}
			// this.addActionMessage("操作成功");
		} catch (Exception e) {
			artFormManager.deleteByIds(entity.getId());
			   this.addActionMessage("请按照美术单考成绩模板表进行编辑，详细错误信息可在日志管理处查看，如果没有权限可联系超级管理员");
			   return INPUT;
		}
		return list();
	}

	@Override
	public String delete() throws Exception {
		try {
			artFormManager.deleteByIds(ids);
			// this.addActionMessage("操作成功");
		} catch (Exception e) {
			// TODO: handle exception
			this.addActionMessage("操作失败，请重新操作");
		}
		return list();
	}

	/**
	 * 改变开放状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String changeConsult() throws Exception {
		artFormManager.changeConsult(status);
		if (status.equals("1"))
			Struts2Utils.renderText("true");
		else
			Struts2Utils.renderText("false");
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id))
			entity = new ArtForm();
		else
			entity = artFormManager.getById(id);

	}

	public ArtFormManager getArtFormManager() {
		return artFormManager;
	}

	@Autowired
	public void setArtFormManager(ArtFormManager artFormManager) {
		this.artFormManager = artFormManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public ArtForm getEntity() {
		return entity;
	}

	public void setEntity(ArtForm entity) {
		this.entity = entity;
	}

	public Page<ArtForm> getPage() {
		return page;
	}

	public void setPage(Page<ArtForm> page) {
		this.page = page;
	}

	public File getArtFile() {
		return artFile;
	}

	public void setArtFile(File artFile) {
		this.artFile = artFile;
	}

	public String getArtFileFileName() {
		return artFileFileName;
	}

	public void setArtFileFileName(String artFileFileName) {
		this.artFileFileName = artFileFileName;
	}

	public ArtDetailManager getArtDetailManager() {
		return artDetailManager;
	}

	@Autowired
	public void setArtDetailManager(ArtDetailManager artDetailManager) {
		this.artDetailManager = artDetailManager;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

}
